package com.example.peibei.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.CircleCrop;
import com.bumptech.glide.request.RequestOptions;
import com.dingtao.common.bean.GetOrderDetail;
import com.dingtao.common.core.DataCall;
import com.dingtao.common.core.WDActivity;
import com.dingtao.common.core.exception.ApiException;
import com.dingtao.common.util.FileCacheUtils;
import com.dingtao.common.util.UIUtils;
import com.example.peibei.R;
import com.example.peibei.service.presenter.CancelOrderPresenter;
import com.example.peibei.service.presenter.GetOrderDetailPresenter;
import com.linxiao.framework.dialog.BottomDialog;

import butterknife.BindView;
import butterknife.OnClick;

public class CancelOrderActivity extends WDActivity {
    @BindView(R.id.iv_head)
    ImageView iv_head;
    @BindView(R.id.tv_name)
    TextView tv_name;
    @BindView(R.id.tv_address)
    TextView tv_address;
    @BindView(R.id.iv_cate)
    TextView iv_cate;
    @BindView(R.id.tv_money)
    TextView tv_money;
    @BindView(R.id.tv_resson)
    TextView tv_resson;
    @BindView(R.id.et_order)
    EditText et_order;
    private GetOrderDetailPresenter getOrderDetailPresenter;
    private CancelOrderPresenter cancelOrderPresenter;
    private String token;
    private String consumeOrderId;
    private String reason = "协商一致退款";

    @Override
    protected int getLayoutId() {
        return R.layout.activity_cancel_order;
    }

    @Override
    protected void initView() {
        Intent intent = getIntent();
        Bundle bundle = intent.getExtras();
        consumeOrderId = bundle.getString("consumeOrderId");
        token = bundle.getString("token");
        getOrderDetailPresenter = new GetOrderDetailPresenter(new OrderPswCall());
        getOrderDetailPresenter.reqeust("Bearer " + token, consumeOrderId, "1");
        cancelOrderPresenter = new CancelOrderPresenter(new CancelCall());
    }

    @OnClick(R.id.bt_submit)
    public void submit() {
        if (TextUtils.isEmpty(et_order.getText().toString())) {
            cancelOrderPresenter.reqeust("Bearer " + token, consumeOrderId, reason);
        } else {
            cancelOrderPresenter.reqeust("Bearer " + token, consumeOrderId, et_order.getText().toString());
        }
    }

    @OnClick(R.id.rl_cancel_type)
    public void cancelType() {
        showDialog();
    }

    @OnClick(R.id.iv_back)
    public void back() {
        finish();
    }

    private void showDialog() {
        View dialogView = LayoutInflater.from(this).inflate(R.layout.dialog_choose_cancel_type, null);
        BottomDialog bottomDialog = new BottomDialog(this);
        bottomDialog.setContentView(dialogView);
        bottomDialog.setCanceledOnTouchOutside(true);
        bottomDialog.show();

        LinearLayout ll_one = dialogView.findViewById(R.id.ll_one);
        LinearLayout ll_two = dialogView.findViewById(R.id.ll_two);
        LinearLayout ll_three = dialogView.findViewById(R.id.ll_three);
        TextView tv_other = dialogView.findViewById(R.id.tv_other);
        ll_one.setOnClickListener(v -> {
            tv_resson.setText("协商一致退款");
            reason = "协商一致退款";
            bottomDialog.dismiss();
        });
        ll_two.setOnClickListener(v -> {
            tv_resson.setText("服务态度差");
            reason = "服务态度差";
            bottomDialog.dismiss();
        });
        ll_three.setOnClickListener(v -> {
            tv_resson.setText("实际与描述不符");
            reason = "实际与描述不符";
            bottomDialog.dismiss();
        });
        tv_other.setOnClickListener(v -> {
            tv_resson.setText("其他");
            reason = "其他";
            bottomDialog.dismiss();
        });
    }

    class OrderPswCall implements DataCall<GetOrderDetail> {

        @Override
        public void success(GetOrderDetail data, Object... args) {
            if (data != null) {
                Glide.with(CancelOrderActivity.this).load(data.getImgUrl()).apply(RequestOptions.bitmapTransform(new CircleCrop())).into(iv_head);
                tv_address.setText(data.getArea());
                iv_cate.setText(data.getCategory());
                tv_money.setText(data.getPrice());
                tv_name.setText(data.getNickName());
            }
        }

        @Override
        public void fail(ApiException e, Object... args) {
        }
    }

    class CancelCall implements DataCall<String> {

        @Override
        public void success(String data, Object... args) {
            UIUtils.showToastSafe("订单已取消");
            finish();
        }

        @Override
        public void fail(ApiException e, Object... args) {
            UIUtils.showToastSafe(e.getDisplayMessage());
        }
    }

    @Override
    protected void destoryData() {

    }
}
